package com.kingyea.app.market.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2017/12/23 18:23
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;

    /**
     * ApplicationContextUtil类在common模块里边时，不能：ApplicationContextUtil.getContext.getBean,需要用我们定义的ApplicationContextUtil.getBean
     */
    public static ApplicationContext getContext() {
        return context;
    }

    public static <T> T getBean(Class<T> clazz) {
        ApplicationContext applicationContext = getContext();
        return applicationContext.getBean(clazz);
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        ApplicationContextUtil.context = context;
    }
}